package com.englisheveryday.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.englisheveryday.consants.SQLConstants;
import com.englisheveryday.response.SystemUpdateResponse;
import com.englisheveryday.util.UUIDGenerator;
import com.englisheveryday.vo.SystemUpdateVO;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-9-16 上午10:59:54
 * @version 1.1
 */
@SuppressWarnings("unchecked")
public class SystemUpdateDAO {

   private JdbcTemplate jdbcTemplate;
	
	public SystemUpdateDAO(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate = jdbcTemplate;
	}
	
	//查询
	public SystemUpdateResponse get_systemUpdate(){
		List<SystemUpdateResponse> list = jdbcTemplate.query(SQLConstants.get_systemupdate, new RowMapper(){
			@Override
			public SystemUpdateResponse mapRow(ResultSet rs, int rowNum) throws SQLException {
				SystemUpdateResponse response = new SystemUpdateResponse();
				response.setNowversion(rs.getInt("nowversion"));
				response.setUrl(rs.getString("url"));
				return response;
			}
		});
		
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	//分页查询
	public List<SystemUpdateVO> query_systemUpdate(int page,int count){
		return jdbcTemplate.query(SQLConstants.query_systemupdate,new Object[]{page,count} ,new RowMapper(){
			@Override
			public SystemUpdateVO mapRow(ResultSet rs, int rowNum) throws SQLException {
				SystemUpdateVO vo = new SystemUpdateVO();
				vo.setNowversion(rs.getInt("nowversion"));
				vo.setUrl(rs.getString("url"));
				vo.setId(rs.getString("id"));
				vo.setCreatetime(rs.getLong("createtime"));
				return vo;
			}
		});
	}
	
	
	//添加
	public int save_systemUpdate(int nowversion,String url){
		return jdbcTemplate.update(SQLConstants.save_systemupdate, new Object[]{
		    UUIDGenerator.generate(),
		    nowversion,
		    url,
		    new Date().getTime()
		});
	}
	
	//删除
	public int delete_systemupdate(String id){
		return jdbcTemplate.update(SQLConstants.delete_systemupdate, id);
	}
}

